﻿using System;
using System.Collections.Generic;
using CookComputing.XmlRpc;
//
using PerceptiveMCAPI.Types;
//
namespace PerceptiveMCAPI.Methods
{
	public class listMergeVarUpdate : Api_BaseExecute
	{

		private new listMergeVarUpdateInput Input { get; set; }
		private listMergeVarUpdateOutput Output { get; set; }
		// =============================================== constructors
		public listMergeVarUpdate() { }
		// ---------
		public listMergeVarUpdate( listMergeVarUpdateInput input ) { Input = input; }
		// =============================================== Execute
		public listMergeVarUpdateOutput Execute()
		{
			// empty constructor & Execute() is bad
			if ( Input == null )
				throw new ArgumentException( "Input class not provided" );
			// do the deed
			Output = Execute( Input );
			return Output;
		}
		// ---------------------------------------------
		public listMergeVarUpdateOutput Execute( listMergeVarUpdateInput input )
		{
			Input = input;
			Output = new listMergeVarUpdateOutput( Input );
			ValidationIsOK = true;
			//------------------------- validate it
			if ( Input.api_Validate )
			{
				Output.api_ValidatorMessages = ValidateInput.Validate( Input );
				ValidationIsOK = ValidateInput.IsOK( Output.api_ValidatorMessages );
			}
			//------------------------- do it
			if ( ValidationIsOK )
			{
				ApiRun( Input, input.parms.apikey );  // execute API & format output
			}
			// ---
			return Output;
		}

		#region ================================================================= API Calls
		// ---------------------------------------------------------- XmlRpc
		protected override void ExecuteXmlRpc()
		{
			try
			{
				XmlRpcStruct options = FormatInput();
				// ---------------------------------------
				bool rpcResult = apiXmlRpc.api.listMergeVarUpdate( Input.parms.apikey, Input.parms.id, Input.parms.tag, options );
				Output.Format( rpcResult );
			}
			catch ( XmlRpcFaultException fex )
			{
				Output.api_ErrorMessages.Add( apiException.FormatError( fex ) );
			}
			catch ( Exception ex )
			{
				Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}
		private XmlRpcStruct FormatInput()
		{
			XmlRpcStruct opts = new XmlRpcStruct();
			opts.Add( "req", Input.parms.options.req );
			opts.Add( "public", Input.parms.options.@public );
			opts.Add( "show", Input.parms.options.show );
			opts.Add( "default_value", Input.parms.options.default_value ?? "" );
			if ( Input.parms.options.choices.Count > 0 )
			{
				string[] choices = new string[Input.parms.options.choices.Count];
				Input.parms.options.choices.CopyTo( choices );
				opts.Add( "choices", choices );
			}
			//
			return opts;
		}

		// ---------------------------------------------------------- Serial Post
		protected override void ExecuteSerialPost()
		{
			try
			{
				Dictionary<string, string> dict = new Dictionary<string, string>();
				dict.Add( "apikey", Input.parms.apikey );
				dict.Add( "id", Input.parms.id );
				dict.Add( "tag", Input.parms.tag );

				dict.Add( "options[req]", Input.parms.options.req.ToString() );
				dict.Add( "options[public]", Input.parms.options.@public.ToString() );
				dict.Add( "options[show]", Input.parms.options.show.ToString() );
				if ( !string.IsNullOrEmpty( Input.parms.options.default_value ) )
					dict.Add( "options[default_value]", Input.parms.options.default_value );

				int i = -1;
				foreach ( string item in Input.parms.options.choices )
				{
					i++;
					dict.Add( string.Format( "options[choices][{0}]", i ), item );
				}
				// -- do it
				string api_Request = "";
				string api_Response = apiSerial.ProcessRequest( Input, dict, out api_Request );
				Output.Format( api_Request, api_Response );
			}
			catch ( Exception ex )
			{
				Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}

		#endregion
	}
}
